/*
 * CObject.h
 *
 *  Created on: 2011-5-4
 *      Author: subo
 */

#ifndef COBJECT_H_
#define COBJECT_H_
#include "string.h"
#include <iostream>
using namespace std;
#define DECLARE_DYNAMIC(class_name)\
	public:\
		static CRuntimeClass class##class_name;\
		virtual CRuntimeClass* GetRuntimeClass() const;

#define DECLARE_DYNCREATE(class_name) \
		DECLARE_DYNAMIC(class_name) \
		static CObject* CreateObject();

#define RUNTIME_CLASS(class_name) \
	(&class_name::class##class_name)

#define IMPLEMENT_DYNAMIC(class_name,base_class_name) \
		CObject* class_name::CreateObject() \
		{ return new class_name; } \
		_IMPLMEMENT_RUNTIMECLASS(class_name,base_class_name,class_name::CreateObject)

#define _IMPLMEMENT_RUNTIMECLASS(class_name,base_class_name,pfnNew) \
		static char _lpsz##class_name[] = #class_name; \
	CRuntimeClass class_name::class##class_name = { \
			_lpsz##class_name,RUNTIME_CLASS(base_class_name),NULL,pfnNew}; \
  static AFX_CLASSINIT _init_##class_name(&class_name::class##class_name); \
        CRuntimeClass* class_name::GetRuntimeClass() const \
                { return &class_name::class##class_name; } \

#define DYNCREATE_OBJECT(class_name) \
    CRuntimeClass::Load(class_name);

class CObject;
struct CRuntimeClass
{

	char* m_lpszClassName;
	CRuntimeClass* m_pBaseClass;
	CRuntimeClass* m_pNextClass;
	static CRuntimeClass* pFirstClass; // start of class list

	CObject* (*m_pfnCreateObject)();
	CObject* CreateObject();
	static CObject* Load(const char* lpszClassName);
};

struct AFX_CLASSINIT
{
	AFX_CLASSINIT(CRuntimeClass* pNewClass);
};
class CObject
{
public:
	CObject();
	virtual ~CObject();

public:
	bool IsKindOf(const CRuntimeClass* pClass) const;
	virtual void SayHello();DECLARE_DYNAMIC(CObject)
};

#endif /* COBJECT_H_ */
